# Start log file for script -----------------------------------------------
TeachingDemos::txtStart("./log files/figure4_ideowhite_prep.txt")

# Load Data ---------------------------------------------------------
# individual data
load("./data/impdat1920.Race.WeekWeight.rdata")
load("./data/impdat20.Race.WeekWeight.rdata")

vars <- c("year", "week", "date_mdy", "ideo3_lab", "age_cat4", "edu_cat4", "race3", "sex",
          "census_region", "broughtwebsite2", "WeekWeights", "D_biep.White_Good_all")
impdat19_sub <- subset(impdat1920.WeekWeight, select = vars)
impdat20_sub <- subset(impdat20.WeekWeight, select = vars)

impdat_comb <- rbind(impdat19_sub, impdat20_sub)


# Models ----------------------------------
m_week1920_ideo <- lm(D_biep.White_Good_all ~ 
                 as.factor(week)*year*ideo3_lab +
                 age_cat4 + edu_cat4 + sex +
                 census_region + broughtwebsite2,
               data = impdat_comb, weights = WeekWeights,
               subset = (date_mdy != "2019-05-25" & date_mdy != "2020-05-25") & race3 == "White")
summary(m_week1920_ideo)

# Prediction Data Sets ------------------------------------------------
weeks19 <- names(table(impdat19_sub$week[which(!is.na(impdat19_sub$WeekWeights))]))
weeks20 <- names(table(impdat20_sub$week[which(!is.na(impdat20_sub$D_biep.White_Good_all))]))
pred_weeks <- weeks20[weeks20 %in% weeks19]

pdat_con <- data.frame(week = pred_weeks,
                   year = c(rep(2019, length(pred_weeks)), rep(2020, length(pred_weeks))),
                   ideo3_lab = "conservative",
                   age_cat4 = "18-29",
                   edu_cat4 = "Post-Grad",
                   sex = "f",
                   census_region = "South",
                   broughtwebsite2 = "Assignment school/work")

pdat_mod <- data.frame(week = pred_weeks,
                       year = c(rep(2019, length(pred_weeks)), rep(2020, length(pred_weeks))),
                       ideo3_lab = "moderate",
                       age_cat4 = "18-29",
                       edu_cat4 = "Post-Grad",
                       sex = "f",
                       census_region = "South",
                       broughtwebsite2 = "Assignment school/work")

pdat_lib <- data.frame(week = pred_weeks,
                       year = c(rep(2019, length(pred_weeks)), rep(2020, length(pred_weeks))),
                       ideo3_lab = "liberal",
                       age_cat4 = "18-29",
                       edu_cat4 = "Post-Grad",
                       sex = "f",
                       census_region = "South",
                       broughtwebsite2 = "Assignment school/work")



# Save DF for Plot -----------------------------------------------------------------
preds_con <- predict(m_week1920_ideo, pdat_con, se.fit = T)
preds_mod <- predict(m_week1920_ideo, pdat_mod, se.fit = T)
preds_lib <- predict(m_week1920_ideo, pdat_lib, se.fit = T)

p_dat <- data.frame(year = c(rep("2019", length(pred_weeks)), rep("2020", length(pred_weeks))),
                    week = pred_weeks,
                    pred = c(preds_con$fit, preds_mod$fit, preds_lib$fit),
                    se = c(preds_con$se.fit, preds_mod$se.fit, preds_lib$se.fit),
                    ideo = c(rep("Conservative", length(pred_weeks)*2), 
                             rep("Moderate", length(pred_weeks)*2),
                             rep("Liberal", length(pred_weeks)*2)))
p_dat$ideo <- factor(p_dat$ideo,
                     levels = c("Conservative", "Moderate", "Liberal"))
p_dat$week <- as.Date(paste(2020, p_dat$week, 1, sep="-"), "%Y-%U-%u")
p_dat$week[c(1,54,107,160, 213, 266)] <- "2020-01-01" # from 00 pred_weeks
p_dat$pred[which(p_dat$week == "2020-11-23" & p_dat$year == "2020")] <- NA
p_dat$se[which(p_dat$week == "2020-11-23" & p_dat$year == "2020")] <- NA
p_dat$pred[which(p_dat$week == "2020-11-30" & p_dat$year == "2020")] <- NA
p_dat$se[which(p_dat$week == "2020-11-30" & p_dat$year == "2020")] <- NA

p_dat_wIdeo <- p_dat


# End log file for script -------------------------------------------------
TeachingDemos::txtStop()